package com.homeinns.crs;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.homeinns.dao.FrequencePersonDao;
import com.homeinns.dao.HotelRmDao;
import com.homeinns.model.Employee;
import com.homeinns.model.EmployeeList;
import com.homeinns.model.FreqencePersonList;
import com.homeinns.model.FrequencePerson;
import com.homeinns.model.HotelRm;
import com.homeinns.model.HotelRmList;
import com.homeinns.service.HotelRmService;
import com.homeinns.service.HotelService;

import deng.qiya.util.LoggerHelper;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	@Autowired
	private HotelRmDao hotelRmDao;
	
	@Autowired
	private HotelService hotelService;
	
	@Autowired(required=false)
	private Jaxb2Marshaller jaxbMarshaller;
	
	@Autowired
	private HotelRmService hotelRmService;
	
	@Autowired
	private FrequencePersonDao frequencePersonDao;
	
	//http://localhost:8080/crs/0210/2013-5-21/2013-5-22/all/all/all/availrm.xml
	@RequestMapping(value = "/{cityCode}/{arrDate}/{depDate}/{brand}/{type}/{regionLandmark}/availrm", method = RequestMethod.GET)
	public String hotelrm(Model model,@PathVariable String cityCode,@PathVariable String arrDate,
			@PathVariable String depDate,@PathVariable String brand, @PathVariable String type,@PathVariable String regionLandmark){
		logger.info(LoggerHelper.paramString(cityCode,arrDate,depDate,brand,type,regionLandmark));
		List<HotelRm> list = hotelRmDao.getHotelRmByCondition(cityCode, arrDate, depDate, brand,type,regionLandmark);
		logger.info("HotelRm list size=" + list.size());
		hotelRmService.calAvailRoom(list);
		model.addAttribute("hotelRmList",new HotelRmList(list));
		return "";
	}
	
	@RequestMapping(value = "/{cityCode}/{arrDate}/{depDate}/{brand}/{type}/{regionLandmark}/newavailrm", method = RequestMethod.GET)
	public String newHotelrm(Model model,@PathVariable String cityCode,@PathVariable String arrDate,
			@PathVariable String depDate,@PathVariable String brand, @PathVariable String type,@PathVariable String regionLandmark){
		logger.info(LoggerHelper.paramString(cityCode,arrDate,depDate,brand,type,regionLandmark));
		//List<HotelRm> list = hotelRmDao.getHotelRmByCondition(cityCode, arrDate, depDate, brand,type,regionLandmark);
		Map<String, HotelRm> hotelRmMap = hotelService.getHotelAva(cityCode, arrDate, depDate, brand, type, regionLandmark);
		logger.info("HotelRm list size=" + hotelRmMap.size());
		model.addAttribute("hotelRmList",new HotelRmList(new ArrayList<HotelRm>(hotelRmMap.values())));
		return "";
	}	
	
	
	//http://localhost:8080/crs/021004/2013-5-28/2013-5-29/availrm.json
	@RequestMapping(value = "/{hotelcd}/{arrDate}/{depDate}/availrm",method = RequestMethod.GET)
	public String getHotelrmByHotelcd(Model model, @PathVariable String hotelcd,@PathVariable String arrDate, @PathVariable String depDate){
		logger.info(LoggerHelper.paramString(hotelcd,arrDate,depDate));
		List<HotelRm> list = hotelRmDao.getHotelRmsByHotelCd(hotelcd, arrDate, depDate);
		hotelRmService.calAvailRoom(list);
		model.addAttribute("hotelRmList",new HotelRmList(list));
		return "";
	}
	
	@RequestMapping(value="/reservation/{orderno}",method=RequestMethod.GET)
	public String getResByOrderNo(Model model, @PathVariable String orderno){
		return "";
	}
	
	//get city region by city code
	@RequestMapping(value="/{cityCode}/region",method=RequestMethod.GET)
	public String getCityRegion(Model model,@PathVariable String cityCode){
		//TODO
		return "";
	}
	
	@RequestMapping(value="/{cityCode}/landmark",method=RequestMethod.GET)
	public String getCityLandmark(Model model, @PathVariable String cityCode){
		//TODO
		return "";
	}
	
	@RequestMapping(value="/{cityCode}/metro",method=RequestMethod.GET)
	public String getCityMetro(Model model, @PathVariable String cityCode) {
		//TODO not do now
		return null;
	}

	@RequestMapping(value="/{cityCode}/regionlandmark",method=RequestMethod.GET)
	public String getCityRegionLandmark(Model model, @PathVariable String cityCode){
		return null;
	}

	//get region and land mark of hotel
	@RequestMapping(value="/{hotelcd}/regionlandmark",method=RequestMethod.GET)
	public String getRegionLandmarkOfHotel(Model model, @PathVariable String hotelcd){
		return null;
	}
	
	//frequence person management
	@RequestMapping(value="/freqperson",method=RequestMethod.POST)
	public String addFreqPerson(Model model,HttpServletRequest request) throws IOException{
		String body = IOUtils.toString(request.getInputStream(),"utf-8");
		logger.info("body="+body);
		Source source = new StreamSource(new StringReader(body));
		FrequencePerson e = (FrequencePerson)jaxbMarshaller.unmarshal(source);
		frequencePersonDao.addFrequencePerson(e.getName(), e.getPhone(), e.getUserId());
		List<FrequencePerson> list = frequencePersonDao.getFrequencePersons(e.getUserId());
		model.addAttribute("freqPersonList", new FreqencePersonList(list));
		return "";
	}
	
	@RequestMapping(value="/freqperson/{userId}",method=RequestMethod.GET)
	public String getFreqPersons(Model model, @PathVariable String userId){
		List<FrequencePerson> list = frequencePersonDao.getFrequencePersons(userId);
		model.addAttribute("freqPersonList", new FreqencePersonList(list));
		return "";
	}
	
	@RequestMapping(value="/freqperson/{id}",method=RequestMethod.DELETE)
	public String delFreqPerson(Model model,@PathVariable String id){
		frequencePersonDao.delFrequencePerson(id);
		return "";
	}
	
	
	//blower is test
	@RequestMapping(value="/employee/{id}",method=RequestMethod.GET)
	public String getEmployee(Model model,@PathVariable String id){
		logger.info("parameter id = "+id);
		Employee e = Employee.getOneEmployee();
		model.addAttribute("employee", e);
		return "";
	}
	
	@RequestMapping(value="/employee",method=RequestMethod.POST)
	public String addEmployee(Model model,HttpServletRequest request) throws IOException{
		String body = IOUtils.toString(request.getInputStream(),"utf-8");
		logger.info("body="+body);
		Source source = new StreamSource(new StringReader(body));
		logger.info("source="+source);
		if(source!=null){
			Employee e = (Employee)jaxbMarshaller.unmarshal(source);
			logger.info("get employee = "+ e.toString());
		}
		EmployeeList elist = EmployeeList.getEmployList();
		model.addAttribute("elist",elist);
		return "";
	}
	
	@RequestMapping(value="/employee",method=RequestMethod.GET)
	public String getAllEmployee(Model model){
		EmployeeList elist = EmployeeList.getEmployList();
		model.addAttribute("elist",elist);
		return "";
	}
	
}
